using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Script.Library.Ability;
using UnityEngine;
using UnityEngine.UI;

public class SonarController : MonoBehaviour
{
	private class Eater
	{
		public List<Icon> icons = new List<Icon>();

		public List<Enemy> creatures;

		public int amountOfActivated;

		public Eater(List<Enemy> creatures)
		{
			this.creatures = creatures;
		}
	}

	private class Icon
	{
		public bool dangerous = true;

		public GameObject gameObject;

		public Image image;

		public Transform transform;

		public Icon(GameObject gameObject, Image image, Transform transform)
		{
			this.gameObject = gameObject;
			this.image = image;
			this.transform = transform;
		}

		public void SetActive(bool value)
		{
			gameObject.SetActive(value);
		}

		public void Opaque()
		{
			if (image.color != Color.white)
			{
				image.color = Color.white;
			}
		}

		public void Transparent()
		{
			if (image.color != Color.clear)
			{
				image.color = Color.clear;
			}
		}
	}

	private static class Limit
	{
		public static int maxX;

		public static int maxY;

		public static int minX;

		public static int minY;
	}

	[Header("Requirement")]
	public CanvasGroup canvasGroup;

	public GameObject iconPrefab;

	public new Transform transform;

	[Header("Requirement")]
	public Sprite dangerous;

	public Sprite notDangerous;

	private Eater meatEater = new Eater(CreatureController.meatEaters);

	private Eater plantEater = new Eater(CreatureController.plantEaters);

	private Sonar sonar;

	private List<Icon> icons = new List<Icon>();

	private int amountOfActivated;

	private List<Collider2D> colliders;

	private int waiting;

	public void Begin(Sonar sonar)
	{
		this.sonar = sonar;
		int num = (int)(iconPrefab.GetComponent<RectTransform>().sizeDelta.x / 2f);
		Limit.maxX = 960 - num;
		Limit.maxY = 540 - num;
		Limit.minX = num - 960;
		Limit.minY = num - 540;
	}

	private void OnEnable()
	{
		canvasGroup.alpha = 1f;
		StartCoroutine(Duration());
	}

	private void Update()
	{
		UpdateIconsPosition(meatEater);
		if (sonar.plantEater)
		{
			if (sonar.creature.meatEater)
			{
				UpdateIconsPosition(plantEater);
			}
			else
			{
				UpdatePlantIcons();
			}
		}
	}

	private Icon CreateIcon()
	{
		Transform transform = Main.CanvasChildNew(this.transform, iconPrefab);
		return new Icon(transform.gameObject, transform.GetComponent<Image>(), transform);
	}

	private IEnumerator Duration()
	{
		yield return new WaitForSeconds(sonar.duration);
		float time = 1f;
		while (time >= 0f)
		{
			canvasGroup.alpha = time;
			time -= Time.deltaTime * 3f;
			yield return Info.wait;
		}
		sonar.End();
	}

	private void UpdateIcon(Icon icon, Creature creature)
	{
		SetIconStatus(icon, creature);
		Vector3 position = creature.GetPosition();
		position = SetIconPosition(icon, position);
	}

	private static Vector3 SetIconPosition(Icon icon, Vector3 position)
	{
		position = Main.WorldToCanvasPosition(position);
		icon.transform.localPosition = new Vector3(Mathf.Clamp(position.x, Limit.minX, Limit.maxX), Mathf.Clamp(position.y, Limit.minY, Limit.maxY));
		return position;
	}

	private void SetIconStatus(Icon icon, Creature creature)
	{
		bool flag = creature.carrion.IsDangerous(sonar.creature);
		bool flag2 = creature.carrion.IsEatable(sonar.creature);
		if (!icon.dangerous)
		{
			if (flag)
			{
				icon.image.sprite = dangerous;
				icon.dangerous = true;
				icon.Opaque();
			}
			else if (!flag2)
			{
				icon.Transparent();
			}
			else
			{
				icon.Opaque();
			}
		}
		else if (flag2)
		{
			if (sonar.plantEater)
			{
				icon.image.sprite = notDangerous;
				icon.dangerous = false;
			}
		}
		else if (!flag)
		{
			icon.Transparent();
		}
		else
		{
			icon.Opaque();
		}
	}

	private void UpdateIconsPosition(Eater eater)
	{
		int count = eater.creatures.Count;
		int count2 = eater.icons.Count;
		for (int i = count2; i < count; i++)
		{
			Enemy creature = eater.creatures[i];
			Icon icon = CreateIcon();
			eater.icons.Add(icon);
			eater.amountOfActivated++;
			UpdateIcon(icon, creature);
		}
		for (int j = eater.amountOfActivated; j < count2; j++)
		{
			eater.icons[j].SetActive(true);
			eater.amountOfActivated++;
		}
		for (int k = 0; k < count2; k++)
		{
			if (k < count)
			{
				UpdateIcon(eater.icons[k], eater.creatures[k]);
				continue;
			}
			eater.icons[k].SetActive(false);
			eater.amountOfActivated--;
		}
	}

	private void UpdatePlantIcons()
	{
		if (waiting-- == 0)
		{
			Physics2D.queriesHitTriggers = true;
			colliders = Physics2D.OverlapCircleAll(sonar.creature.GetPosition(), CreatureController.distance, 256).ToList();
			Physics2D.queriesHitTriggers = false;
			waiting = 4;
		}
		else
		{
			colliders.RemoveAll((Collider2D o) => !o);
		}
		int count = colliders.Count;
		int count2 = icons.Count;
		for (int i = count2; i < count; i++)
		{
			Icon icon = CreateIcon();
			icons.Add(icon);
			amountOfActivated++;
			icon.image.sprite = notDangerous;
			SetIconPosition(icon, colliders[i].transform.position);
		}
		for (int j = amountOfActivated; j < count2; j++)
		{
			icons[j].SetActive(true);
			amountOfActivated++;
		}
		for (int k = 0; k < count2; k++)
		{
			if (k < count)
			{
				SetIconPosition(icons[k], colliders[k].transform.position);
				continue;
			}
			icons[k].SetActive(false);
			amountOfActivated--;
		}
	}
}
